<?php

class ControllerLoader {
	public $e;
	public $WhatToDo = false;
	private $contDir = 'app/';

	public function __construct($obj, $controllername, $element) {
		try {
		    $controllername = trim($controllername);
		    $name = explode('_',$controllername);
		    $name = end($name);
		    $controllername = preg_replace('/_/','/',$controllername);
		    
		    if (is_file ( $this->contDir . $controllername . '.php' )) {
				$this->LoadModelDb();
				include $this->contDir . $controllername . '.php';
				$obj->design->assign ( 'app_name', $controllername );
				$contr = new $name($element);
				$contr->getWort ( $obj );
				$this->WhatToDo = true;				
			} else {
				throw new Exception ( "Controller nicht gefunden !" );
			}
		}catch( Exception $e ) {
			if($e->getMessage() == 'next'){
				$this->WhatToDo = false;
			}else{
				echo $e->getMessage();
			}
		}
	}
	private function LoadModelDb() {
		$Dirs = array("./app/db","./app/until");

		foreach ($Dirs as $dir){
			$handle = opendir ( $dir );
			while ( false !== ($file = readdir ( $handle )) )
			{
				if ($file != "." && $file != "..")
				{
					if (!is_dir ( $dir . "/" . $file ))
					{
						$compl = $dir . "/" . $file;
						include_once $compl;
					}
				}
			}
			closedir ( $handle );
		}
	}

}